/*
 * @Author: 追逐的娃 kangchao_php@163.com
 * @Date: 2024-04-30 17:16:03
 * @LastEditors: 追逐的娃 kangchao_php@163.com
 * @LastEditTime: 2024-05-06 17:12:09
 * @FilePath: \huijie-admin\src\pinia\modules\system.js
 * @Description: 系统公共缓存
 * 
 */
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { getInfo, logOut } from '@/api/user'
import { useRouter } from 'vue-router'

export const useSystemStore = defineStore('hj-system',
    () => {
        //存储token
        const token = ref('')
        //设置token
        const setToken = (tokenVal) => {
            token.value = tokenVal
        }
        //导航打开还是关闭
        const isCollapse = ref(false)
        //设置导航打开/关闭
        const setIsCollapse = () => {
            isCollapse.value = !isCollapse.value
        }

        //存储用户信息
        let userInfo = ref({})

        //获取用户信息
        const getUserInfo = () => {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    userInfo.value = res
                    resolve(res)
                })
                    .catch(err => reject(err))
            })
        }
        let router = useRouter()
        //退出登录
        const logout = () => {

            //1\移除token
            token.value = ''
            //2、清空用户信息
            userInfo.value = {}
            router.push({path:'/login'})
        }
        //返回变量或者函数
        return {
            token,
            setToken,
            isCollapse,
            setIsCollapse,
            getUserInfo,
            userInfo,
            logout
        }
    },
    //开启默认存储所有数据
    {
        persist: true
    }
)